/*
  Table

  This class performs utility functions for the PHP class of the same name and
  its derivatives (notably ManagedTable).
*/
function Table() {
  util.addEvent(window, 'load', this.load.bind(this));
  this.checkBoxes = [];
  this.lastCheckBoxClicked = false;
}

/*
  When the page loads, focus the search box, get all the checkboes in an array,
  and reset the mass operator selectbox.
*/
Table.prototype.load = function() {
  this.focusSearchBox();
  this.checkBoxes = document.getElementsByName('table-checkboxes');
  var self = this;
  for (var i = 0; i < this.checkBoxes.length; i++) {
    this.checkBoxes[i].onclick = function (e) {
      if (!e) e = window.event;
      if (!e.target) e.target = e.srcElement;
      self.clickCheckBox(e.target, e.shiftKey);
    };
  }
  /* In case the user hits the back button */
  if ($('table-mass-operator')) {
    $('table-mass-operator').selectedIndex = 0;
  }
};

/*
  Changes the number of rows displayed on the page. The URL is generated by PHP
  and applied to the page here.
*/
Table.prototype.changeRowMax = function (url) {
  window.location = url;
};

/*
  If there are any checkboxes unchecked, check them all. Otherwise uncheck all.
*/
Table.prototype.toggleCheckBoxes = function() {
  var atleastOneUnChecked = false;
  for (var i = 0; i < this.checkBoxes.length; i++) {
    atleastOneUnChecked = atleastOneUnChecked || !this.checkBoxes[i].checked;
  }
  for (var i = 0; i < this.checkBoxes.length; i++) {
    this.checkBoxes[i].checked = atleastOneUnChecked;
  }
};

/*
  If the user clicks a checkbox, then holds the shift key and clicks a second
  checkbox, apply the state of the second checkbox to all checkboxes in between.
*/
Table.prototype.clickCheckBox = function(checkBox, shiftKey) {
  if (this.lastCheckBoxClicked && shiftKey) {
    var boxesInBetween = [];
    var position = 0;
    for (var i = 0; i < this.checkBoxes.length; i++) {
      if ((this.checkBoxes[i] == this.lastCheckBoxClicked) ||
          (this.checkBoxes[i] == checkBox)) {
        position++;
        boxesInBetween.push(this.checkBoxes[i]);
      } else if (position == 1) {
        boxesInBetween.push(this.checkBoxes[i]);
      }
    }
    /* If we found the start and end checkBoxes */
    if (position == 2) {
      for (var i = 0; i < boxesInBetween.length; i++) {
        boxesInBetween[i].checked = checkBox.checked;
      }
    }
  }
  this.lastCheckBoxClicked = checkBox; 
};

/*
  Focuses the searchbox.
*/
Table.prototype.focusSearchBox = function() {
  if ($('table-searchbox')) {
    $('table-searchbox').focus();
  }
};

/*
  Submits the search form.
*/
Table.prototype.submitSearchForm = function() {
  $('table-form').submit();
  return false;
};

/*
  Creates the drop menu when a user clicks on the dropdown button.
*/
Table.prototype.showButtonDropMenu = function(element, links) {
  var menu = new DropMenu(element, DROPMENU_MODE_BOTTOM);
  for (var item in links) {
    menu.addItem(item, links[item]);
  }
  menu.draw();
};

/*
  Generates a list of all the values of checked checkboxes and sends them to the
  specified URL. The URL supplied should end in '='. 
*/
Table.prototype.massOperate = function(url) {
  var values = [];
  for (var i = 0; i < this.checkBoxes.length; i++) {
    if (this.checkBoxes[i].checked) {
      values.push(this.checkBoxes[i].value);
    }
  }
  window.location = url + values.join('+');
};

/* Create our Table singleton */
var table = new Table();
